package com.ybdz.scanner.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.ybdz.scanner.R;
import com.ybdz.scanner.adapter.chuKuLiShiAdapter;
import com.ybdz.scanner.base.BaseActivity;
import com.ybdz.scanner.bean.chuKuBean;
import com.ybdz.scanner.event.ObjEvent;
import com.ybdz.scanner.utils.ListUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**出库历史界面
 * Created by gs on 2018/3/20.
 */

public class ChuKuLiShiActivity extends BaseActivity implements View.OnClickListener{

    private LinearLayout back;
    private FrameLayout home_o;
    private RecyclerView rv_o;
    private QMUIEmptyView empty_o;
    private Button back_home_o;
    private String outFormCode;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_output_history);
        EventBus.getDefault().register(this);

        initView();

        initRecycleView();
    }

    private void initView() {
        back = findViewById(R.id.ll_back_o);
        home_o = findViewById(R.id.home_o);
        rv_o = findViewById(R.id.rv_o);
        empty_o = findViewById(R.id.empty_o);
        back_home_o = findViewById(R.id.back_home_o);

        back.setOnClickListener(this);
        home_o.setOnClickListener(this);
        back_home_o.setOnClickListener(this);

        //出库单号
        TextView outformcode = (TextView) findViewById(R.id.outformcode);
        if (outFormCode == null){
            outformcode.setText("");
        }else {
            outformcode.setText(outFormCode);
        }

    }


    /**
     * 接收EventBus传递的数据
     *
     * @param onResponeEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getOutFormCode(ObjEvent onResponeEvent) {
        if (onResponeEvent.getId().equals("outFormCode")) {
            outFormCode = (String) onResponeEvent.getObj();
        }
    }

    /**
     * 初始化recycleview
     */
    private void initRecycleView() {
        List<chuKuBean> list = ListUtil.getChuKuList();

        rv_o.setLayoutManager(new LinearLayoutManager(this));

        if (list == null || list.size() == 0) {
            empty_o.show(false, "暂时没有数据", null, null, null);
        } else {
            chuKuLiShiAdapter adapter = new chuKuLiShiAdapter(this,list);
            rv_o.setAdapter(adapter);
            empty_o.hide();
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_back_o:                  //返回
                finish();
                break;
            case R.id.home_o:                     //返回首页
                startActivity(new Intent(ChuKuLiShiActivity.this, MainActivity.class));
                finish();
                break;
            case R.id.back_home_o:                //返回首页
                startActivity(new Intent(ChuKuLiShiActivity.this, MainActivity.class));
                finish();
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
